<?php

namespace App\Http\Controllers;

use App\Models\Account;
use App\Models\Message;
use App\Models\VerifyCode;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use Carbon\Carbon;
use Illuminate\Support\Facades\Validator;

class MessageController extends Controller {
    public function index() {
        return response()->json( [ 'success'=>"message" ], 201 );
    }

    // 创建消息
    public function createMessage(Request $request){
        $request->validate( [
            'msgtitle' => 'required|string',
            'msgsubtitle' => 'required|string',
            'msgcontent' => 'required|string'
        ] );
        $account = $request->account;
        // 创建新消息记录
        $message = Message::create( [
            'account_id'=> $account->id,
            'message_title' => $request->msgtitle,
            'message_sub_title' => $request->msgsubtitle,
            'message_content' => $request->msgcontent
        ] );

        return response()->json( ["isok"=>"1",
        'data'=>[
            'message' => $message
            ]
        ], 201 );
    }
    
    // 获取全部消息
    public function getAllMessages(Request $request){
        $message = Message::orderBy('id', 'desc')->get(); 
        return response()->json( ["isok"=>"1",
        'data'=>[
            'message' => $message
            ]
        ], 201 );
    }

    public function messageList(Request $request) {
        $account = $request->account;
        $message = Message::where([
            "account_id"=>$account->id
        ])->orderBy('id', 'desc')->get();
        return response()->json( ["isok"=>"1",
        'data'=>[
            'message' => $message
            ]
        ], 201 );
    }

    public function deleteMessage(Request $request) {
        $account = $request->account;
        $message = Message::where([
            "id"=>$request->message_id
        ])->first();
        if ($message == null) {
            return response()->json( [
                "isok"=>"0",
                'errors' => 'message not find'
            ], 422 );
        }
        if ($message->account_id != $account->id) {
            return response()->json( [
                "isok"=>"0",
                'errors' => 'message not find'
            ], 422 );
        }
        $message = Message::where([
            "account_id"=>$account->id,
            "id"=>$request->message_id
        ])->delete();
        $message = Message::where([
            "account_id"=>$account->id
        ])->get();
        return response()->json( ["isok"=>"1",
        'data'=>[
            'message' => $message
            ]
        ], 201 );
    }

    public function editMessage(Request $request) {
        $request->validate( [
            'msgtitle' => 'required|string',
            'msgsubtitle' => 'required|string',
            'msgcontent' => 'required|string'
        ] );

        $account = $request->account;
        $message = Message::where([
            "account_id"=>$account->id,
            "id"=>$request->message_id
        ])->first();
        if ($message == null) {
            return response()->json( [
                "isok"=>"0",
                'errors' => 'message not find'
            ], 422 );
        }
        $message->message_title = $request->msgtitle;
        $message->message_sub_title = $request->msgsubtitle;
        $message->message_content = $request->msgcontent;
        $message->save();
        $message = Message::where([
            "id"=>$request->message_id
        ])->get();
        return response()->json( ["isok"=>"1",
        'data'=>[
            'message' => $message
            ]
        ], 201 );
    }
}
?>